package lanou.giftsay_01.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import lanou.giftsay_01.R;
import lanou.giftsay_01.bean.SelectGiftAllBean;
import lanou.giftsay_01.tools.DBTool;

/**
 * Created by dllo on 16/8/24.
 */
public class SelectGiftRvAdapter extends RecyclerView.Adapter<SelectGiftRvAdapter.MySGViewHolder> {
 private Context context ;
 private SelectGiftAllBean selectGiftAllBean ;
    int a ;

    public void setA(int a) {
        this.a = a;
        notifyDataSetChanged();
    }

    public void setSelectGiftAllBean(SelectGiftAllBean selectGiftAllBean) {
        this.selectGiftAllBean = selectGiftAllBean;
        notifyDataSetChanged();
    }

    public SelectGiftRvAdapter(Context context) {
        this.context = context;
    }

    @Override
    public MySGViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_rv_seletgift, parent ,false);
        MySGViewHolder mySGViewHolder = new MySGViewHolder(view);
        return mySGViewHolder;
    }

    @Override
    public void onBindViewHolder(MySGViewHolder holder, int position) {
        ArrayList<SelectGiftAllBean.DataBean.ItemsBean> otherBean  = new ArrayList<>();
        for (int i = 0; i < selectGiftAllBean.getData().getItems().size();  i++) {
            otherBean.add(selectGiftAllBean.getData().getItems().get(i));
        }

        switch (a) {
            case 0:
                holder.tv.setText(selectGiftAllBean.getData().getItems().get(position).getName());
                Picasso.with(context).load(selectGiftAllBean.getData().getItems().get(position).getCover_image_url()).into(holder.iv);
                holder.like.setText(String.valueOf(selectGiftAllBean.getData().getItems().get(position).getFavorites_count()));
                holder.title.setText(selectGiftAllBean.getData().getItems().get(position).getPrice());

                break;
            case 1:
                Collections.sort(otherBean, new HotpxBean());
                holder.tv.setText(otherBean.get(position).getName());
                Picasso.with(context).load(otherBean.get(position).getCover_image_url()).into(holder.iv);
                holder.like.setText(String.valueOf(otherBean.get(position).getFavorites_count()) );
                holder.title.setText(otherBean.get(position).getPrice());
                break;
            case 3:
                Collections.sort(otherBean , new PriceBean());
                holder.tv.setText(otherBean.get(position).getName());
                Picasso.with(context).load(otherBean.get(position).getCover_image_url()).into(holder.iv);
                holder.like.setText(String.valueOf(otherBean.get(position).getFavorites_count()) );
                holder.title.setText(otherBean.get(position).getPrice());

                break;
            case 2:
               Collections.sort(otherBean ,new PriceSBean());
                holder.tv.setText(otherBean.get(position).getName());
                Picasso.with(context).load(otherBean.get(position).getCover_image_url()).into(holder.iv);
                holder.like.setText(String.valueOf(otherBean.get(position).getFavorites_count()) );
                holder.title.setText(otherBean.get(position).getPrice());

                break;
        }

    }

    @Override
    public int getItemCount() {
        return selectGiftAllBean != null ? selectGiftAllBean.getData().getItems().size() : 0;
    }

    public class MySGViewHolder extends RecyclerView.ViewHolder {
        TextView tv ;
        TextView title ;
        TextView like ;
        ImageView iv ;
        public MySGViewHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.select_item_name);
            title = (TextView) itemView.findViewById(R.id.selectgift_item_title);
            like = (TextView) itemView.findViewById(R.id.selectgift_like_count);
            iv = (ImageView) itemView.findViewById(R.id.selectgift_item_iv);
        }
    }

    class  HotpxBean implements Comparator {
        @Override
        public int compare(Object lhs, Object rhs) {
            SelectGiftAllBean.DataBean.ItemsBean s1 = (SelectGiftAllBean.DataBean.ItemsBean) lhs;
            SelectGiftAllBean.DataBean.ItemsBean s2 = (SelectGiftAllBean.DataBean.ItemsBean) rhs;
            return new Integer(s2.getFavorites_count()).compareTo(s1.getFavorites_count());
        }
    }
    class  PriceBean implements Comparator {
        @Override
        public int compare(Object lhs, Object rhs) {
            SelectGiftAllBean.DataBean.ItemsBean s1 = (SelectGiftAllBean.DataBean.ItemsBean) lhs;
            SelectGiftAllBean.DataBean.ItemsBean s2 = (SelectGiftAllBean.DataBean.ItemsBean) rhs;
            return new Double(s2.getPrice()).compareTo(Double.valueOf(s1.getPrice()));
        }
    }
    class  PriceSBean implements Comparator {
        @Override
        public int compare(Object lhs, Object rhs) {
            SelectGiftAllBean.DataBean.ItemsBean s1 = (SelectGiftAllBean.DataBean.ItemsBean) lhs;
            SelectGiftAllBean.DataBean.ItemsBean s2 = (SelectGiftAllBean.DataBean.ItemsBean) rhs;
            return new Double(s1.getPrice()).compareTo(Double.valueOf(s2.getPrice()));
        }
    }
}
